package com.cloud.workTest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.cloud.bean.PatientInfo;
import com.cloud.bean.YapiInterface;
import com.cloud.bean.YapiItem;
import com.cloud.common.Result;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.fastjson.JSON.parseObject;


@Slf4j
public class JsonTest {
    public static void main(String[] args) {
//        String JsonString = " {\"success\":true,\"resultCode\":\"100\",\"msg\":\"成功\",\"data\":[{\"gmtModify\":1622098512000,\"gmtCreate\":1622098512000,\"id\":30400,\"corpId\":20201,\"corpName\":\"阜阳人民医院(南区)\",\"userId\":996771,\"patientId\":1000026071,\"patientName\":\"吴文辉\",\"idType\":1,\"idNo\":\"330***********4312\",\"guarderIdNo\":\"\",\"status\":100,\"billNo\":\"12868242\",\"billType\":\"已结算\",\"billFee\":100,\"deptName\":\"方便门诊\",\"selfFee\":0,\"insurFee\":0,\"oppatNo\":\"0001785671\",\"cardNo\":\"000000010410\",\"transNo\":\"12868242\",\"payDate\":\"2021-05-27\",\"unionId\":3401,\"billName\":\"已结算\"}],\"startTime\":1629108816777,\"timeConsum\":3723,\"traceId\":\"0a1000061629108813027289913244\"}";
//        Result result = parseObject(JsonString, Result.class);

//        String patientInfoStr = "{\"cost\":345.78, \"date\":\"2021-11-29 23:00:00\"}";
//        PatientInfo patientInfo = parseObject(patientInfoStr, PatientInfo.class);
//
//        System.out.println(patientInfo);
//
//        List<String> strArray = JSON.parseArray("[\"市南区\",\"市北区\",\"李沧区\",\"崂山区\",\"城阳区\",\"西海岸新区\",\"即墨区\",\"平度市\",\"莱西市\",\"胶州市\"]", String.class);
//
//        strArray.forEach( area -> System.out.println(area));
//
//        System.out.println("http://192.168.88.106:16007".substring(7).split(":")[0] + ":9100");

        Gson gson = new Gson();
        // 读取文件内容到字符串
        File file = new File("C:\\Users\\86159\\Downloads\\api.json");
        if (!file.exists()) {
            try {
                File parentFile = file.getParentFile();
                if(!parentFile.exists()) {
                    parentFile.mkdirs();
                }
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        StringBuilder content = new StringBuilder();
        try (FileReader fileReader = new FileReader(file)) {
            int character;
            while ((character = fileReader.read()) != -1) {
                content.append((char) character);
            }
        } catch (Exception e) {
        }

        String originalJson = content.toString();

        List<YapiInterface> yapiInterfaces = gson.fromJson(originalJson,
                new TypeToken<List<YapiInterface>>(){}.getType());
        Connection conn;
        try {
            DriverManager.setLoginTimeout(20);
            conn = DriverManager.getConnection("jdbc:mysql://192.168.88.102:19306/supervise2?useSSL=false", "root", "Aa123456");

            Integer i = 1;

            for (YapiInterface yapiInterface : yapiInterfaces) {
                List<YapiItem> yapiItems = yapiInterface.getList();
                for (YapiItem yapiItem : yapiItems) {
                    String sql = "insert into `os_interface` (id, path, method, description) values("+ i +",'"
                            + yapiItem.getPath() + "','" +yapiItem.getMethod()  + "','" + yapiItem.getTitle() + "')";
                    PreparedStatement ps = conn.prepareStatement(sql);
                    ps.execute(sql);
                    System.out.println(sql);
                    i++;
                }

            }


        } catch (Exception e) {
            log.error("error",e);
        }


    }

}
